package com.epoint.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.epoint.common.Result;
import com.epoint.entity.BookInfo;
import com.epoint.service.BookInfoService;

/**
 * Servlet implementation class BookInfoEditAction
 */
@WebServlet("/bookinfoeditaction")
public class BookInfoEditAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
	BookInfoService bookInfoService = new BookInfoService();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public BookInfoEditAction() {
		super();
	}

	/**
	 * 获取后端BookInfo数据响应给页面
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		BookInfo bookInfo = new BookInfo();

		String bookId = request.getParameter("bookId");
		
		// 根据bookId获取BookInfo数据
		bookInfo = bookInfoService.findById(bookId);
		
		response.getWriter().print(JSONObject.toJSON(bookInfo));
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String data = request.getParameter("data");
		// 将json字符串转为BookInfo对象
		JSONObject jsonObject = JSONObject.parseObject(data);
		BookInfo bookInfo = JSON.toJavaObject(jsonObject, BookInfo.class);

		int flag = bookInfoService.updateBookInfo(bookInfo);

		String returnCode = Result.returnCode(flag);
		response.getWriter().print(returnCode);
		
	}

}
